<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Promise</title>
</head>
<body>
<script type="text/javascript">
    function action(resolve, reject, name) {
        console.log("Action Start" + name);

        for (let i = 0; i < 999999999; i++) {

        }

        let num = Math.random();
        console.log(num);

        if (num > 0.5) {
            resolve(num);
        } else {
            reject(num);
        }

        console.log("Action End" + name);

        // return num;
    }

    // setTimeout(action, 1);

    // let p01 = new Promise(function (resolve, reject) {
    //     setTimeout(action, 1, resolve, reject, "01");
    // });
    //
    // let p02 = new Promise(function (resolve, reject) {
    //     setTimeout(action, 1, resolve, reject, "02");
    // });
    // console.log(action());

    // p01.then(function (dat) {
    //     console.log("Success:" + dat);
    // }, function (dat) {
    //     console.log("Failure:" + dat);
    // });

    // p01.then(function (dat) {
    //     console.log("Success:" + dat);
    // }).catch(function (dat) {
    //     console.log("Failure:" + dat);
    // }).finally(function () {
    //     console.log("Finally!");
    // });

    // Promise.all([p01, p02]).then(function (dat) {
    //     console.log("Success:" + dat);
    // }).catch(function (dat) {
    //     console.log("Failure:" + dat);
    // }).finally(function () {
    //     console.log("Finally!");
    // });

    // Promise.race([p01, p02]).then(function (dat) {
    //     console.log("Success:" + dat);
    // }).catch(function (dat) {
    //     console.log("Failure:" + dat);
    // }).finally(function () {
    //     console.log("Finally!");
    // });

    Promise.resolve().then(function () {
        for (let i = 0; i < 999999999; i++) {

        }

        console.log(Math.random());
    });

    // Promise.reject().catch(function () {
    //     for (let i = 0; i < 999999999; i++) {
    //
    //     }
    //
    //     console.log(Math.random());
    // });

    console.log("Main Thread");

</script>
</body>
</html>